/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.DaiLyDAO;
import DTO.DaiLyDTO;
import UTILS.ToolOfGUI;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author HuuTri
 */
public class DaiLyBUS {
    
    /**
     * get String like database style
     * @param maloaidaily
     * @param madaily
     * @return 
     */
    public static String getStringOfMaDaiLyAndMaLoaiDaiLy(String maloaidaily, String madaily) {
        return maloaidaily + "." + madaily;
    }
    
    /**
     * split database string to view
     * @param string
     * @return 
     */
    public static String[] getMaDaiLyAndMaLoaiDaiLyFromString(String string) {
        return string.split("\\.");
    }
    
    /**
     * check value madaily
     * @param maloaidaily
     * @param madaily
     * @return 
     */
    public static boolean checkMaDaiLy(String id, String maloaidaily, String madaily) {
        DaiLyDTO daily = new DaiLyDTO();
        daily.setMaDaiLy(madaily);
        daily.setMaLoaiDaiLy(maloaidaily);
        daily.setId(Integer.parseInt(id));
        
        return DaiLyDAO.checkMaDaiLy(daily);
    }
    
    /**
     * get List maquan by combobox
     * @return 
     */
    public static DefaultComboBoxModel getListMaQuanByComboBoxModel() {
        List<String> listData = DaiLyDAO.getListMaQuan();
        int row = listData.size();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        
        for (int i = 0; i < row; i++ ) {
            model.addElement(listData.get(i));
        }
        
        return model;
    }
    
    /**
     * Lấy danh sách các Mã Loại Dại Lý
     * @return 
     */
    public static DefaultComboBoxModel getListMaLoaiDaiLyByComboBoxModel() {
        List<String> listData = DaiLyDAO.getListMaLoaiDaiLy();
        int row = listData.size();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        
        for (int i = 0; i < row; i++) {
            model.addElement(listData.get(i));
        }
        
        return model;
    }
    
    /**
     * Cập nhật dữ liệu
     * @param id
     * @param madaily
     * @param tendaily
     * @param maloaidaily
     * @param maquan
     * @param tienno
     * @param dienthoai
     * @param diachi
     * @param email
     * @param ngaytiepnhan 
     */
    public static boolean updateRecord(String id, String madaily, String tendaily,
            String maloaidaily, String maquan, String tienno, String dienthoai,
            String diachi, String email, String ngaytiepnhan) {
     DaiLyDTO daily = new DaiLyDTO(
                Integer.parseInt(id),
                madaily,
                tendaily,
                maloaidaily,
                maquan,
                Integer.parseInt(tienno),
                dienthoai,
                diachi,
                email,
                ngaytiepnhan
                );
        return DaiLyDAO.update(daily);
    
    }
    
    /**
     * Xóa một record khỏi bảng
     * @param id
     * @param madaily
     * @param tendaily
     * @param maloaidaily
     * @param maquan
     * @param tienno
     * @param dienthoai
     * @param diachi
     * @param email
     * @param ngaytiepnhan 
     */
    public static  boolean  deleteRecord(String id, String madaily, String tendaily,
            String maloaidaily, String maquan, String tienno, String dienthoai,
            String diachi, String email, String ngaytiepnhan) {
    
         DaiLyDTO daily = new DaiLyDTO(
                Integer.parseInt(id),
                madaily,
                tendaily,
                maloaidaily,
                maquan,
                Integer.parseInt(tienno),
                dienthoai,
                diachi,
                email,
                ngaytiepnhan
                );
        return DaiLyDAO.delete(daily);
    }
    
    /**
     * Xóa một record khỏi data
     * @param id
     * @param madaily
     * @param tendaily
     * @param maloaidaily
     * @param maquan
     * @param tienno
     * @param dienthoai
     * @param diachi
     * @param email
     * @param ngaytiepnhan 
     */
    public static boolean insertRecord(String id, String madaily, String tendaily,
            String maloaidaily, String maquan, String tienno, String dienthoai,
            String diachi, String email, String ngaytiepnhan) {
        
        DaiLyDTO daily = new DaiLyDTO(
                Integer.parseInt(id),
                madaily,
                tendaily,
                maloaidaily,
                maquan,
                Integer.parseInt(tienno),
                dienthoai,
                diachi,
                email,
                ngaytiepnhan
                );
        return DaiLyDAO.insert(daily);
    }
    
    /**
     * Lấy dữ liệu từ table 
     * @param tableModel
     * @param row
     * @param columnName
     * @return 
     */
    public static Object getValue(TableModel tableModel, int row, String columnName) {
        int index = 0;       
        Object value = " ";
        if (row < 0) return " ";
        switch(columnName) {
            case DaiLyDTO.ID: 
                value = tableModel.getValueAt(row, index);
                break;
            case DaiLyDTO.MA_DAI_LY: 
                value = tableModel.getValueAt(row, index + 1);
                break;
            case DaiLyDTO.TEN_DAI_LY: 
                value = tableModel.getValueAt(row, index + 2);
                break;
            case DaiLyDTO.MA_LOAI_DAI_LY: 
                value = tableModel.getValueAt(row, index + 3);
                break;
            case DaiLyDTO.MA_QUAN: 
                value = tableModel.getValueAt(row, index + 4);
                break;
            case DaiLyDTO.TIEN_NO: 
                value = tableModel.getValueAt(row, index + 5);
                break;
            case DaiLyDTO.DIEN_THOAI: 
                value = tableModel.getValueAt(row, index + 6);
                break;
            case DaiLyDTO.DIA_CHI: 
                value = tableModel.getValueAt(row, index + 7);
                break;
            case DaiLyDTO.EMAIL: 
                value = tableModel.getValueAt(row, index + 8);
                break;
            case DaiLyDTO.NGAY_TIEP_NHAN: 
                value = tableModel.getValueAt(row, index + 9);
                break;
                
        }
        if (value != null)
            return value;
        else 
            return "";
    }
    
    public static void setValue(TableModel tableModel, int row, String columnName, Object value) {
        int index = 0;        
        if (row < 0) return;
        switch(columnName) {
            case DaiLyDTO.ID: tableModel.setValueAt(value, row, index); 
                break;
            case DaiLyDTO.MA_DAI_LY: tableModel.setValueAt(value, row, index + 1); 
                break;
            case DaiLyDTO.TEN_DAI_LY: tableModel.setValueAt(value, row, index + 2); 
                break;
            case DaiLyDTO.MA_LOAI_DAI_LY: tableModel.setValueAt(value, row, index + 3); 
                break;
            case DaiLyDTO.MA_QUAN: tableModel.setValueAt(value, row, index + 4); 
                break;
            case DaiLyDTO.TIEN_NO: tableModel.setValueAt(value, row, index + 5); 
                break;
            case DaiLyDTO.DIEN_THOAI: tableModel.setValueAt(value, row, index + 6); 
                break;
            case DaiLyDTO.DIA_CHI: tableModel.setValueAt(value, row, index + 7); 
                break;
            case DaiLyDTO.EMAIL: tableModel.setValueAt(value, row, index + 8); 
                break;
            case DaiLyDTO.NGAY_TIEP_NHAN: tableModel.setValueAt(value, row, index + 9); 
                break;
        }
    }
    /**
     * Lấy thông tin id mới
     */
    public static String getNewId() {
        return String.valueOf(DaiLyDAO.getNewId());
    }
    
    /**
     * Lấy thông tin bảng hiển thi;
     */
    public static DefaultTableModel getTableModel() {
        return DaiLyDAO.getTableModel();
    }
    
}